The Code Node allows you to build and execute custom Python functions.Use the Code node instead of the function calling code block in LLM Tools if you want a more deterministic pipeline (In function calling your LLM will reason to determine if it makes sense to call the function).
Use Case: Webscraping with BeautifulSoupimport requestsfrom bs4 import BeautifulSoupimport jsonimport redef main(): url = "https://example.com/product-page" # Replace with actual product page URL response = requests.get(url) # Parse the HTML content soup = BeautifulSoup(response.text, 'html.parser') # Assume the product data is embedded in a script tag or fetched via AJAX script_tag = soup.find('script', text=re.compile('window.__PRODUCT_DATA__')) if script_tag: # Extract JSON string from the script tag json_text = re.search(r'window\.__PRODUCT_DATA__\s*=\s*({.*?});', script_tag.string).group(1) else: # If script tag not found, assume data was fetched via AJAX ajax_url = "https://example.com/api/product" # Replace with actual AJAX endpoint ajax_response = requests.get(ajax_url) json_text = ajax_response.text # Instance check to determine if input is a JSON string or a Python dictionary if isinstance(json_text, str): product_data = json.loads(json_text) # Parse the JSON string into a Python dictionary elif isinstance(json_text, dict): product_data = json_text # Already a dictionary, no need to parse else: raise ValueError("Input must be a JSON string or a Python dictionary") # Extract specific product details product_name = product_data.get('name', 'Unknown') product_price = product_data.get('price', 'Unavailable') output = { "Product Name": product_name, "Product Price": product_price } return json.dumps(output, indent=4)